/* 	Default value of cvar auto_buy_type
	0 - nothing
	1 - if user is buying automaticly he will pay additional money
		realCost = cost + auto_buy_cost_val
	2 - if user is buying automaticly he will pay additional % of real cost 
		realCost = cost + (cost * auto_buy_cost_val)
*/
#define CVAR_AUTO_BUY_TYPE_DEF "2"

/*	Additional cost for faster buying items
	if(auto_buy_cost_type == 1) it is integer
	if(auto_buy_cost_type == 2) it is float
*/
#define CVAR_AUTO_BUY_COST_VAL_DEF "0.1"

/*	Default value for cvar loose_on_death
	This is used only by td_pocket.inl
*/
#define CVAR_LOOSE_ON_DEATH_DEF "1"

/*	Maximum noumber of dealers
	If you have only 5 dealers change it to 5
*/
#define MAX_DEALERS 10

/*	Maximum number of players on server +1
	if server has 25 slots you can change it to 26
*/
#define MAX_PLAYERS 33

/*	This is prefix added to cvars and natives
	For example:
		if prefix is "dd" than auto_buy_cost_val cvar will be dd_auto_buy_cost_val
	It should be unique if you have multiple dealers
*/
new const prefix[] = "dd";

/*	This is preifx displayed in chat messages
*/
new const PREFIX_DISP[] = "[Drug Dealers]";

/*	These are items dealer can have
*/
enum _: ITEMS {
	ITEM_WEED,	//Delete this and add your own items
	ITEM_SPEED,
	ITEM_ACID,
	ITEM_AMFETAMINE,
	ITEM_HEROINE,
	ITEM_COCAINE
}


/*	These are items language specific names
	Real names will be taken from dictionary file
*/
new const ITEM_NAMES[ITEMS][] = {
	"WEED",
	"SPEED",
	"ACID",
	"AMFETAMINE",
	"HEROINE",
	"COCAINE"
}

enum _:ML_STRINGS_ENUM {
	mlDealersMenuTitle,
	mlNoAccessToDealer,
	mlCount,
	mlCost,
	mlItemMenuTitle,
	mlItemBought,
	mlNotEnoughMoney,
	mlItemsSold,
	mlCanNotBuy,
	mlPocketMenuTitle,
	mlNotInPocket,
	mlNoItemsInPocket
}

new const ML_STRINGS[ML_STRINGS_ENUM][]={
	"DEALERS_MENU_TITLE" , 	//mlDealersMenuTitle
	"NO_ACCESS_TO_DEALED", 	//mlNoAccessToDealer
	"COUNT", 		//mlCount
	"COST", 			//mlCost
	"ITEMS_MENU_TITLE",	//mlItemMenuTitle
	"ITEM_BOUGHT",		//mlItemBought
	"NOT_ENOUGH_MONEY",	//mlNotEnoughMoney
	"ITEMS_SOLD",		//mlItemsSold
	"CAN_NOT_BUY",		//mlCanNotBuy
	"POCKET_MENU_TITLE",	//mlPocketMenuTitle
	"NOT_IN_POCKET",	//mlNotInPocket
	"NO_ITEMS_IN_POCKET"	//mlNoItemsInPocket
}

#define PLUGIN "Drug Dealers"
#define VERSION "1.0"
#define AUTHOR "Zabijaka"


#include "the_dealers\td_def.inc"
#include "the_dealers\td_main.inl"
#include "the_dealers\td_register.inl"
#include "the_dealers\td_menus.inl"
#include "the_dealers\td_tools.inl"

/*	Comment line below if you do not want to give player pocket
	In pocket player can store items 
*/
#include "the_dealers\td_pocket.inl"

/*	Comment line below if you do not need natives for controling player pocket from other plugins
*/
//#include "the_dealers\td_pocket_natives.inl"

/*	Comment line below if you do not want to give player store
	In store player can store items
*/
//#include "the_dealers\td_store.inl"

/*	Comment line below if you do not need natives for controling dealers and items from other plugins
*/
//#include "the_dealers\td_natives.inl"

#include <fun>
#define DTUG_TASK 100

new gDealerCracker;
new gDealerBadBoy;
new gDealerMafioso;


new playerDrugsTaken[MAX_PLAYERS][ITEMS];
new playerAddictedPoints[MAX_PLAYERS];
new Float:playerDmgReceived[MAX_PLAYERS];
new playerAddicted[MAX_PLAYERS];
new playerLastDrug[MAX_PLAYERS];

new gmsgShake;
new gmsgFade;
enum _:DrugInfoEnum {
	diAddictedPoints,
	diOverdose,
	diHealth,
	Float:diDmgProtect,
	Float:diGravity,
	Float:diSpeed,
	Float:diTime
}

#define ADDICTED_POINTS_PER_ROUND 3
#define POINTS_TO_ADDICTION 11

new DrugInfo[ITEMS][DrugInfoEnum] = {
	/* WEED */ 	{ 2, 5, 20, 0.0, 0.05, 0.0, 20.0},
	/* SPEED */	{ 2, 3, 10, 0.0, 0.2, 0.2, 35.0},
	/* ACID */	{ 3, 2, 25, 0.2, 0.1, 0.1, 30.0},
	/* AMFETAMINE */{ 3, 2, 30, 0.3, 0.1, 0.1, 40.0},
	/* HEROINE */	{ 4, 1, 50, 0.4,  0.0, 0.0, 45.0},
	/* COCAINE */	{ 5, 1, 80, 0.3, 0.2, 0.2, 60.0}
}

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR);
	
	//Register your dictionary with item names and ML_STRINGS
	// ...
	
	//Comment line below if you have all ML_STRINGS in your own dictionary
	register_dictionary("the_dealers.txt");
	
	
	register_def_cvars();
	
	#if defined _td_pocket_included
	plugin_init_pocket();
	#endif
	
	// Add your code here
	// ...
	
	gDealerCracker = td_register_dealer("Cracker", Dra_OnLost, 3, false);
	gDealerBadBoy = td_register_dealer("Bad Boy", Dra_OnLost, 4, false);
	gDealerMafioso = td_register_dealer("Mafioso", Dra_OnLost, 5, false);

	/*
	Add dealer items:
	Cracker have 70 to 80 percent chance to have 4 to 7 portions of weed, each in price between 800 and 1000$
	*/
	td_set_dealer_item_def(gDealerCracker,ITEM_WEED, 4, 7, 70, 80,800, 1000);
	td_set_dealer_item_def(gDealerCracker,ITEM_SPEED, 2, 5, 50, 60,1000, 1500);
	td_set_dealer_item_def(gDealerCracker,ITEM_ACID, 1, 5, 50, 60,1000, 1500);
	
	td_set_dealer_item_def(gDealerBadBoy,ITEM_WEED, 3, 5, 20, 60,500, 800);
	td_set_dealer_item_def(gDealerBadBoy,ITEM_SPEED, 1, 3, 10, 50,900, 1200);
	td_set_dealer_item_def(gDealerBadBoy,ITEM_ACID, 2, 7, 40, 50,700, 1300);
	td_set_dealer_item_def(gDealerBadBoy,ITEM_AMFETAMINE, 1, 4, 50, 70,2000, 4000);
	td_set_dealer_item_def(gDealerBadBoy,ITEM_HEROINE, 1, 3, 30, 60,4000, 7000);

	td_set_dealer_item_def(gDealerMafioso,ITEM_WEED, 3, 5, 20, 60,500, 800);
	td_set_dealer_item_def(gDealerMafioso,ITEM_SPEED, 1, 3, 10, 50,900, 1200);
	td_set_dealer_item_def(gDealerMafioso,ITEM_ACID, 2, 7, 40, 50,700, 1300);
	td_set_dealer_item_def(gDealerMafioso,ITEM_AMFETAMINE, 1, 4, 50, 70,1800, 3500);
	td_set_dealer_item_def(gDealerMafioso,ITEM_HEROINE, 3, 5, 40, 80,4000, 6000);
	td_set_dealer_item_def(gDealerMafioso,ITEM_COCAINE, 1,2, 0, 100,8000, 10000);
	
	register_clcmd("say", "handle_say");
	register_clcmd("say_team", "handle_say");
}

public handle_say(id){
	new said[64];
	
	read_argv(1, said, charsmax(said));
	remove_quotes(said);
	strtolower(said);
	new word[10];
	new drug[16];
	new lngItemName[16];
	strbreak(said, word, charsmax(word), drug,  charsmax(drug));
	if(equal(word, "take") ){
		for(new item = 0;item<ITEMS;item++) {
			lngItemName[0] = '^0';
			formatex(lngItemName, charsmax(lngItemName),"%L", id, ITEM_NAMES[item]);
			if(equali(drug, lngItemName)){
				td_take_from_pocket(id, item);
				return PLUGIN_HANDLED;
			}
		}
	}
	else if(equal(word, "buy")){
		for(new item = 0;item<ITEMS;item++) {
			lngItemName[0] = '^0';
			formatex(lngItemName, charsmax(lngItemName),"%L", id, ITEM_NAMES[item]);
			if(equali(drug, lngItemName)){
				td_quick_buy_item(id, item);
				return PLUGIN_HANDLED;
			}
		}
	}
	else if(equal(word, "drug")){
		if(equali(drug, "dealers")){
			td_dealers_menu(id);
			return PLUGIN_HANDLED;
		}
		if(equali(drug, "info")){
			show_drug_info_motd(id);
			return PLUGIN_HANDLED;
		}
	}
	else if(equal(word, "pocket") && !drug[0]){
		td_pocket_menu(id);
		return PLUGIN_HANDLED;
	}
	return PLUGIN_CONTINUE;
}

stock show_drug_info_motd(id) {
	new motd[1200];
	new lenMax = charsmax(motd);
	new len = formatex(motd, lenMax, "<html><body style=^"background-color: black;color: red^"><h1>%L</h1>", id, "DRUG_INFO");
	len+= formatex(motd[len], lenMax-len,"<h2>%L<br />%L<br />%L</h2>", id,"DRUG_INFO1",id,"DRUG_INFO2",id,"DRUG_INFO3");
	len+= formatex(motd[len], lenMax-len,"<table, border=^"solid^">");
	len += formatex(motd[len], lenMax - len, "<tr><th>%L</th><th>%L</th><th>%L</th><th>%L</th><th>%L</th><th>%L</th><th>%L</th><th>%L</th></tr>",
		id, "TH_DRUG", id, "TH_ADDICTED_POINTS", id, "TH_MAX_PER_ROUND", id, "TH_HP",  id, "TH_LESS_DMG", id, "TH_GRAVITY", id, "TH_SPEED", id, "TH_TIME");
	for(new item=0;item<ITEMS;item++){
		len += formatex(motd[len], lenMax - len,"<tr><td>%L</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td></tr>",
			ITEM_NAMES[item], DrugInfo[item][diAddictedPoints],
			DrugInfo[item][diOverdose], 
			DrugInfo[item][diHealth],
			floatround(DrugInfo[item][diDmgProtect] * 100.0),
			floatround(DrugInfo[item][diGravity] * 100.0),
			floatround(DrugInfo[item][diSpeed] * 100.0),
			floatround(DrugInfo[item][diTime]));
	}
	len += formatex(motd[len], lenMax - len, "</table></body></html>");
	show_motd(id, motd);
}

public plugin_natives(){
	#if defined _td_natives_included
	register_def_natives();
	#endif
	
	#if defined _td_pocket_natives_included
	register_def_pocket_natives();
	#endif
}

public client_connect(id){
	client_connect_main(id);
	
	
	#if defined _td_pocket_included
	client_connect_pocket(id);
	#endif
	
	set_task(30.0, "show_info", id);
}

public show_info(id){
	if(is_user_connected(id)){
		new allDrugs[128];
		formatex(allDrugs, charsmax(allDrugs), "%L, %L, %L, %L, %L, %L",
			id, ITEM_NAMES[ITEM_WEED], 
			id, ITEM_NAMES[ITEM_SPEED],
			id, ITEM_NAMES[ITEM_ACID],
			id, ITEM_NAMES[ITEM_AMFETAMINE],
			id, ITEM_NAMES[ITEM_HEROINE],
			id, ITEM_NAMES[ITEM_COCAINE]);
		switch(random (5)){
			case 0: client_print(id, print_chat, "%s %L drug dealers %L", PREFIX_DISP, id, "SAY_COMMAND", id, "DISP_DRUGS_MENU");
			case 1: client_print(id, print_chat, "%s %L drug info %L", PREFIX_DISP, id, "SAY_COMMAND", id, "DISP_DRUGS_INFO");
			case 2: client_print(id, print_chat, "%s %L take %s %L", PREFIX_DISP, id, "SAY_COMMAND", id, "TAKE_DRUG");
			case 3: client_print(id, print_chat, "%s %L buy %s %L", PREFIX_DISP, id, "SAY_COMMAND", id, "BUY_DRUG");
			case 4: client_print(id, print_chat, "%s %L pocket %s %L", PREFIX_DISP, id, "SAY_COMMAND", id, "DRUGS_IN_POCKET");
		}
		set_task(random_float(30.0, 60.0), "show_info", id);
	}
}

public client_authorized(id){
	td_set_player_dealer(id, gDealerMafioso, bool:is_user_admin(id));
}
/*	Modify this method if you want to disallow buyin some items in some condition
	@params:
		id - 	players id
		item - 	item
	returns:
		true if you want to allow buying item, false othervise
*/
stock bool:td_can_give_item(id, item){
	return true;
}

/*	This method handeles buying items
	For example if you have weapon dealer in this method you should give weapon
*/
stock td_custom_item_get(id, item){
	td_add_to_pocket(id, item);
}



#if defined _td_pocket_included
/*	Override this method to define when player can take item from his pocket
*/
stock bool:td_can_get_item_from_pocket(id, item){
	if(!is_user_alive(id))
		return false;
	
	return true;
}

/*	This method handeles taking item from pocket
*/
stock td_custom_item_get_from_pocket(id, item) {
	playerDrugsTaken[id][item]++;
	
	set_finish_task(id, item);
	
	new health = get_user_health(id);
	health += DrugInfo[item][diHealth];
	set_user_health(id, health);
	
}


stock set_finish_task(id, item){
	new taskId = DTUG_TASK + item*100 + id;
	if(task_exists(taskId)){
		remove_task(taskId);
	}
	
	new param[1];
	param[0] = item;
	set_task(DrugInfo[item][diTime], "finish_drug", taskId, param, 1);
}

public finish_drug(param[], taskId){
	new item = param[0];
	new id = taskId - item*100 - DTUG_TASK;
	
	
}

#endif


#if defined _td_store_included
/*	Override this method to define when player can take item from his store
*/
stock bool:td_can_get_item_from_store(id, item){
	#if defined _td_pocket_included
		return td_can_add_item_to_pocket(id, item) == plcTrue;
	#else
		return td_can_give_item(id, item);
	#endif
}

/*	This method handeles taking item from store
*/
stock td_custom_item_get_from_store(id, item) {
	#if defined _td_pocket_included
		td_add_to_pocket(id, item);
	#else
		td_custom_item_get(id, item);
	#endif
}

#endif


